<template>
  <div>
    <el-dialog :title="title" :visible="visible" width="60%" :modal="false" top="200px" custom-class="customerDialogClass"
      :before-close="dialogVisible" @opened="opens">

      <div class="formList" style="margin-bottom:10px">
        <el-form :inline="true">
          <el-form-item>
            <el-cascader size="large" :options="ops" v-model="selectedOp" @change="handleChange">
            </el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button size="medium" @click="button">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <div id="row1">
          <dv-border-box-9 style="width: 49%; float: left; overflow: hidden ">
            <div ref="chart1" style="height:320px"></div>
          </dv-border-box-9>

          <dv-border-box-9 style="width: 49%; float: right; overflow: hidden">
            <div ref="chart2" style="height:320px"></div>
          </dv-border-box-9>
        </div>

        <div id="row2" style="margin-top:10px">
          <dv-border-box-9 style="width: 49%; float: left; overflow: hidden; margin-top:20px">
            <div ref="chart3" style="height:320px"></div>
          </dv-border-box-9>

          <dv-border-box-9 style="width: 49%; float: right; overflow: hidden; margin-top:20px">
            <div ref="chart4" style="height:320px"></div>
          </dv-border-box-9>
        </div>

      </div>

    </el-dialog>
  </div>

</template>

<script>
import * as echarts from "echarts";
import "echarts-gl";
import { provinceAndCityData } from 'element-china-area-data'
var mychart1;
var mychart2;
var mychart3;
var mychart4;

// 生成扇形的曲面参数方程，用于 series-surface.parametricEquation
function getParametricEquation(startRatio,endRatio,isSelected,isHovered,k) {
  // 计算
  let midRatio=(startRatio+endRatio)/2;

  let startRadian=startRatio*Math.PI*2;
  let endRadian=endRatio*Math.PI*2;
  let midRadian=midRatio*Math.PI*2;

  // 如果只有一个扇形，则不实现选中效果。
  if(startRatio===0&&endRatio===1) {
    isSelected=false;
  }

  // 通过扇形内径/外径的值，换算出辅助参数 k（默认值 1/3）
  k=typeof k!=="undefined"? k:1/3;

  // 计算选中效果分别在 x 轴、y 轴方向上的位移（未选中，则位移均为 0）
  let offsetX=isSelected? Math.cos(midRadian)*0.1:0;
  let offsetY=isSelected? Math.sin(midRadian)*0.1:0;

  // 计算高亮效果的放大比例（未高亮，则比例为 1）
  let hoverRate=isHovered? 1.05:1;

  // 返回曲面参数方程
  return {
    u: {
      min: -Math.PI,
      max: Math.PI*3,
      step: Math.PI/32
    },

    v: {
      min: 0,
      max: Math.PI*2,
      step: Math.PI/20
    },

    x: function(u,v) {
      if(u<startRadian) {
        return (
          offsetX+
          Math.cos(startRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      if(u>endRadian) {
        return (
          offsetX+Math.cos(endRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      return offsetX+Math.cos(u)*(1+Math.cos(v)*k)*hoverRate;
    },

    y: function(u,v) {
      if(u<startRadian) {
        return (
          offsetY+
          Math.sin(startRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      if(u>endRadian) {
        return (
          offsetY+Math.sin(endRadian)*(1+Math.cos(v)*k)*hoverRate
        );
      }
      return offsetY+Math.sin(u)*(1+Math.cos(v)*k)*hoverRate;
    },

    z: function(u,v) {
      if(u<-Math.PI*0.5) {
        return Math.sin(u);
      }
      if(u>Math.PI*2.5) {
        return Math.sin(u);
      }
      return Math.sin(v)>0? 1:-1;
    }
  };
}

// 生成模拟 3D 饼图的配置项
function getPie3D(pieData,internalDiameterRatio) {
  let series=[];
  let sumValue=0;
  let startValue=0;
  let endValue=0;
  let legendData=[];
  let k=
    typeof internalDiameterRatio!=="undefined"?
      (1-internalDiameterRatio)/(1+internalDiameterRatio):
      1/3;

  // 为每一个饼图数据，生成一个 series-surface 配置
  for(let i=0;i<pieData.length;i++) {
    sumValue+=pieData[i].value;

    let seriesItem={
      name: typeof pieData[i].name==="undefined"?
        `series${i}`:pieData[i].name,
      type: "surface",
      parametric: true,
      wireframe: {
        show: false
      },
      pieData: pieData[i],
      pieStatus: {
        selected: false,
        hovered: false,
        k: k
      }
    };

    if(typeof pieData[i].itemStyle!="undefined") {
      let itemStyle={};

      typeof pieData[i].itemStyle.color!="undefined"?
        (itemStyle.color=pieData[i].itemStyle.color):
        null;
      typeof pieData[i].itemStyle.opacity!="undefined"?
        (itemStyle.opacity=pieData[i].itemStyle.opacity):
        null;

      seriesItem.itemStyle=itemStyle;
    }
    series.push(seriesItem);
  }

  // 使用上一次遍历时，计算出的数据和 sumValue，调用 getParametricEquation 函数，
  // 向每个 series-surface 传入不同的参数方程 series-surface.parametricEquation，也就是实现每一个扇形。
  for(let i=0;i<series.length;i++) {
    endValue=startValue+series[i].pieData.value;

    series[i].pieData.startRatio=startValue/sumValue;
    series[i].pieData.endRatio=endValue/sumValue;
    series[i].parametricEquation=getParametricEquation(
      series[i].pieData.startRatio,
      series[i].pieData.endRatio,
      true,
      false,
      1
    );

    startValue=endValue;

    legendData.push(series[i].name);
  }

  // 补充一个透明的圆环，用于支撑高亮功能的近似实现。
  series.push({
    name: "mouseoutSeries",
    type: "surface",
    parametric: true,
    wireframe: {
      show: false
    },
    itemStyle: {
      opacity: 1,
      color: "rgba(18,236,252,.5)"
    },
    parametricEquation: {
      u: {
        min: 0,
        max: Math.PI*2,
        step: Math.PI/20
      },
      v: {
        min: 0,
        max: Math.PI,
        step: Math.PI/1.4
      },
      x: function(u,v) {
        return Math.sin(v)*Math.sin(u)+Math.sin(u);
      },
      y: function(u,v) {
        return Math.sin(v)*Math.cos(u)+Math.cos(u);
      },
      z: function(u,v) {
        return Math.cos(v)>0? 0.1:-0.1;
      }
    }
  });

  // 准备待返回的配置项，把准备好的 legendData、series 传入。
  let option={
    //animation: false,
    legend: {
      show: true,
      data: legendData,
      textStyle: {
        color: '#ffffff',
        fontSize: 12,
        fontWeight: 0
      },
      top: 20
    },
    tooltip: {
      formatter: params => {
        if(params.seriesName!=='mouseoutSeries') {
          return `${params.seriesName}<br/><span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${params.color};"></span>${option.series[params.seriesIndex].pieData.value}`;
        }
      }
    },
    xAxis3D: {
      min: -1.3,
      max: 1.3
    },
    yAxis3D: {
      min: -1.3,
      max: 1.3
    },
    zAxis3D: {
      min: -1.3,
      max: 1.3
    },
    grid3D: {
      show: false,
      boxHeight: 10,
      // top: '30%',
      left: "0",
      bottom: "50%",
      viewControl: {
        //3d效果可以放大、旋转等，请自己去查看官方配置
        alpha: 20,
        // beta: 40,
        rotateSensitivity: 0,
        zoomSensitivity: 0,
        panSensitivity: 0,
        autoRotate: true,
        //   autoRotateSpeed: 5,
        //   autoRotateAfterStill: 10
      },
    },
    series: series
  };
  return option;
}

//温湿度风向折线柱状图
var Data={
  times: ["2019-09-08 00:00:00","2019-09-08 01:00:00","2019-09-08 02:00:00","2019-09-08 03:00:00","2019-09-08 04:00:00","2019-09-08 05:00:00","2019-09-08 06:00:00","2019-09-08 07:00:00","2019-09-08 08:00:00"],
  timeData: ["08/00","01","02","03","04","05","06","07","08"],
  windxData: ["东南","北","东南","东","东南","东南","西","北","南","东南","东南","东南"],
  windsData: ["2.8/2","1.4/1","2.7/2","3/2","2.2/2","1.4/1","1.5/1","1.9/2","1.4/1"],
  temData: [25.4,24.3,24,23,22.1,21.8,22.7,23,25.3],
  rainData: [0,0.1,0.1,0.2,0.1,0,0.1,0,0.1],
  humData: [59,67,70,74,77,77,65,63,60],
}
function windDen(wind) {
  switch(wind) {
    case '北': return 'n';
    case '东北': return 'ne';
    case '东': return 'e';
    case '东南': return 'se';
    case '南': return 's';
    case '西南': return 'sw';
    case '西': return 'w';
    case '西北': return 'nw';
    default: return ''
  }
}


export default {

  data() {
    return {
      //级联选择器
      ops: provinceAndCityData,
      selectedOp: [],

      // 传入数据生成 option
      option5: getPie3D([
        {
          name: '招待所',
          value: 15,
          itemStyle: {
            color: '#00d8ff',
          }
        },{
          name: '宾馆',
          value: 12,
          itemStyle: {
            color: '#018ef1'
          }
        },
        {
          name: '旅游酒店',
          value: 4,
          itemStyle: {
            color: '#f8bc87'
          }
        },
        {
          name: '三星级',
          value: 3,
          itemStyle: {
            color: '#ffa130'
          }
        },
        {
          name: '四星级',
          value: 2,
          itemStyle: {
            color: '#c23531'
          }
        },
        {
          name: '五星级',
          value: 1,
          itemStyle: {
            color: '#fd5d48'
          }
        },
      ],0.59),

      option6: {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#57617B'
            }
          }
        },
        legend: {
          // icon: 'diamond',
          itemGap: 13,
          data: ['好评人次/万','到访人次/万','查询人次/万'],
          top: '3%',
          left: 'center',
          textStyle: {
            fontSize: 12,
            color: '#F1F1F3'
          }
        },
        grid: {
          bottom: '10%'
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },

          axisLabel: {
            margin: 10,
            textStyle: {
              color: "rgba(255,255,255,.6)",
              fontSize: '12',
            }
          },
          splitLine: {
            lineStyle: {
              color: "rgba(255,255,255,0.1)",
            }
          }
        }],
        series: [
          {
            name: '查询人次/万',
            type: 'line',
            smooth: true,
            markLine: { //最大值和最小值
              data: [{
                name: '阈值54',
                yAxis: 54,
                label: {
                  show: 'true',
                  position: 'end',
                  formatter: '增加量阈值54'
                },
                lineStyle: {
                  normal: {
                    width: 2,
                    color: "#FF5D1D",
                  }
                }

              },
              ]
            },
            lineStyle: {
              normal: {
                width: 1
              }
            },
            symbol: 'circle',
            symbolSize: 4,
            itemStyle: {
              normal: {
                color: 'green',
                // borderColor: 'blue', //拐点边框颜色
              }
            },
            data: [50,52,52,52,52,58,52,55,48,52,52,52]
          },{
            name: '到访人次/万',
            type: 'line',
            smooth: true,
            markLine: { //最大值和最小值
              data: [{
                name: '阈值30',
                yAxis: 30,
                label: {
                  show: 'true',
                  position: 'end',
                  formatter: '减少量阈值30'
                },
                lineStyle: {
                  normal: {
                    width: 2,
                    color: "#FF5D1D",
                  }
                }

              },

              ]
            },
            lineStyle: {
              normal: {
                width: 1
              }
            },
            symbol: 'circle',
            symbolSize: 4,
            itemStyle: {
              normal: {
                color: '#00A1EA',
                // borderColor: 'blue', //拐点边框颜色
              }
            },
            data: [30,32,32,32,32,38,32,33,36,32,32,32]
          },{
            name: '好评人次/万',
            type: 'line',
            smooth: true,
            markLine: { //最大值和最小值
              data: [
                {
                  name: '阈值10',
                  yAxis: 10,
                  label: {
                    show: 'true',
                    position: 'end',
                    formatter: '变化量阈值10'
                  },
                  lineStyle: {
                    normal: {
                      width: 2,
                      color: "#FF5D1D",
                    }
                  }
                },

              ]
            },
            lineStyle: {
              normal: {
                width: 1
              }
            },
            symbol: 'circle',
            symbolSize: 4,
            itemStyle: {
              normal: {
                color: 'yellow',
                // borderColor: 'blue', //拐点边框颜色
              }
            },
            data: [10,11,12,13,11,9,11,12,13,11,9,8]
          }]

      },

      option8: {
        // color: colors,
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' },
          formatter: function(params,ticket,callback) {
            let index=params[0].dataIndex;
            let Htm=`${Data.times[index]}<br>
                    温度:${Data.temData[index]}℃<br>
                    降水:${Data.rainData[index]}mm<br>
                    风力:${Data.windsData[index].split('/')[0]}m/s ${Data.windsData[index].split('/')[1]}级风 ${Data.windxData[index]}<br>
                    湿度:${Data.humData[index]}%`
            return Htm;
          }
        },
        grid: {
          top: '25%',
          bottom: '25%',
          left: '20%',
          right: '8%'
        },
        dataZoom: {
          bottom: '14%',
          height: '30',
          dataBackground: {
            lineStyle: { color: '#39977D' },
            areaStyle: { color: 'rgba(57,151,125,1)' }
          },
          borderColor: 'rgba(57,151,125,0.2)',
          textStyle: { color: '#39977D' },
          start: 0,
          end: 100,
          xAxisIndex: [0,1,2,3]
        },
        xAxis: [{
          name: '风力(m/s)/等级',
          type: 'category',
          position: 'bottom',
          offset: 8,
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#000000',
              fontSize: 14,
              backgroundColor: '#FFE282',
              lineHeight: 20,
              padding: [3,3]
            },
            interval: 0
          },
          nameTextStyle: { color: '#585858',padding: [0,0,-53] },
          nameLocation: 'start',
          data: Data.windsData
        },
        {
          name: '风向',
          type: 'category',
          position: 'bottom',
          offset: 40,
          axisTick: { show: false },
          axisLine: { show: false },
          axisLabel: {
            show: true,
            textStyle: { color: '#000000',fontSize: 14,lineHeight: 20 },
            interval: 0,
            formatter: (value) => {
              return '{'+windDen(value)+'| }'+value+'';
            },
            rich: {
              value: {
                lineHeight: 16,
                align: 'left'
              },
              n: {
                height: 16,
                align: 'left',
              },
              ne: {
                height: 16,
                align: 'left',
              },
              e: {
                height: 16,
                align: 'left',
              },
              se: {
                height: 16,
                align: 'left',
              },
              s: {
                height: 16,
                align: 'left',
              },
              sw: {
                height: 16,
                align: 'left',
              },
              w: {
                height: 16,
                align: 'left',
              },
              nw: {
                height: 16,
                align: 'left',
              }
            }
          },
          nameTextStyle: { color: '#585858',padding: [0,0,-38] },
          nameLocation: 'start',
          data: Data.windxData
        },
        {
          type: 'category',
          axisLine: { show: false },
        },
        {
          type: 'category',
          position: 'bottom',
          offset: 80,
          axisTick: { alignWithLabel: true,textStyle: { color: '#707070' } },
          data: Data.timeData
        }
        ],
        yAxis: [{
          type: 'value',
          name: '温度(°C)',
          scale: true,
          position: 'left',
          offset: 54,
          axisTick: { lineStyle: { color: '#FF6863' },inside: true },
          nameTextStyle: { color: '#FF6863' },
          axisLabel: { color: '#FF6863' },
          splitLine: { show: false }
        },
        {
          type: 'value',
          name: '降水(mm)',
          position: 'left',
          axisTick: { lineStyle: { color: '#6EAB40' },inside: true },
          nameTextStyle: { color: '#6EAB40' },
          axisLabel: { color: '#6EAB40' },
          splitLine: { show: false }
        },
        {
          type: 'value',
          name: '相对湿度(%)',
          scale: true,
          position: 'right',
          axisTick: { lineStyle: { color: '#3BB5F5' },inside: true },
          nameTextStyle: { color: '#3BB5F5' },
          axisLabel: { color: '#3BB5F5' },
          splitLine: { show: false }
        }
        ],
        series: [{
          name: '温度',
          type: 'line',
          step: false,
          label: { normal: { show: true,position: 'top' } },
          data: Data.temData
        },
        {
          name: '降水',
          type: 'bar',
          yAxisIndex: 1,
          color: '#bee6b2',
          barWidth: '50%',
          label: { normal: { show: true,position: 'top' } },
          data: Data.rainData
        },
        {
          name: '相对湿度',
          type: 'line',
          step: false,
          yAxisIndex: 2,
          color: '#3BB5F5',
          label: { normal: { show: true,position: 'top' } },
          data: Data.humData
        }
        ]
      },
      option9: {
        "normal": {
          "top": 200,
          "left": 300,
          "width": 500,
          "height": 400,
          "zIndex": 6,
          "backgroundColor": ""
        },
        "color": ["rgba(245, 166, 35, 1)","rgba(19, 173, 255, 1)"],
        "tooltip": {
          "show": true,
          "trigger": "item"
        },
        "legend": {
          "show": true,
          "icon": "circle",
          "left": "43%",
          "top": "90%",
          "orient": "horizontal",
          "textStyle": {
            "fontSize": 16,
            "color": "#fff"
          },
          "data": ["2020","2021"]
        },
        "radar": {
          "center": ["50%","45%"],
          "radius": "70%",
          "startAngle": 90,
          "splitNumber": 4,
          "shape": "polygon",
          name: {
            textStyle: {
              color: '#fff',
              fontSize: 16,
              padding: [-10,-12]
            }
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: '#0d6dba',
              opacity: 0.1
            }
          },
          "axisLabel": {
            "show": false,
            "fontSize": 18,
            "color": "#fff",
            "fontWeight": "normal"
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#4f8bbe',
              opacity: 1
            }
          },
          splitLine: {

            lineStyle: {
              color: '#4f8bbe',
              opacity: 0.5
            }
          },
          "indicator": [{
            "name": "维也纳",
            "max": 8
          },{
            "name": "汉庭",
            "max": 8
          },{
            "name": "如家",
            "max": 8
          },{
            "name": "全季",
            "max": 8
          },{
            "name": "格林豪泰",
            "max": 8
          },{
            "name": "7天",
            "max": 8
          },{
            "name": "锦江之星",
            "max": 8
          },{
            "name": "尚客优",
            "max": 8
          },{
            "name": "麓枫",
            "max": 8
          },{
            "name": "其他",
            "max": 8
          },]
        },
        "series": [
          {
            "name": "2020",
            "type": "radar",
            "symbol": "circle",
            "symbolSize": 10,
            "areaStyle": {
              "normal": {
                "color": "rgba(245, 166, 35, 0.4)"
              }
            },
            itemStyle: {
              color: 'rgba(245, 166, 35, 1)',
              borderColor: 'rgba(245, 166, 35, 0.3)',
              borderWidth: 10,
            },
            "lineStyle": {
              "normal": {
                "type": "dashed",

                "color": "rgba(245, 166, 35, 1)",
                "width": 2
              }
            },
            "data": [
              [8,5,5,8,5,8,4,3,5,4]
            ]
          },{
            "name": "2021",
            "type": "radar",
            "symbol": "circle",
            "symbolSize": 10,
            "itemStyle": {
              "normal": {
                color: 'rgba(19, 173, 255, 1)',
                "borderColor": "rgba(19, 173, 255, 0.4)",
                "borderWidth": 10
              }
            },
            "areaStyle": {
              "normal": {
                "color": "rgba(19, 173, 255, 0.5)"
              }
            },
            "lineStyle": {
              "normal": {
                "color": "rgba(19, 173, 255, 1)",
                "width": 2,
                "type": "dashed"
              }
            },
            "data": [
              [6,6,5,6,7,4,8,3,5,5]
            ]
          }]
      },
    }
  },



  props: {
    // 标题
    title: {
      type: String,
      default: '提示'
    },
    // 弹框控制
    visible: {
      type: Boolean,
      default: false
    },
    // 文本
    text: {
      type: String,
      default: '文本信息'
    },
    // 接收的数据
    // data: {
    //     type: Object,
    //     default: () => {}
    // },
    // 是否居中
    center: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    dialogVisible() {
      this.$emit('update:visible',false);
    },
    opens() {
      this.$nextTick(() => {
        this.pie1()
        // initChart()
      })
    },
    pie1() {
      mychart1=echarts.init(this.$refs.chart1)
      mychart1.setOption(this.option6)
      mychart2=echarts.init(this.$refs.chart2)
      mychart2.setOption(this.option5)
      mychart3=echarts.init(this.$refs.chart3)
      mychart3.setOption(this.option9)
      mychart4=echarts.init(this.$refs.chart4)
      mychart4.setOption(this.option8)
    },
    button() {
      //chart1
      mychart1.clear()
      let randomArr1=[]; let randomArr2=[]; let randomArr3=[];
      for(let i=0;i<12;i++) {
        randomArr1.push(Math.floor(Math.random()*14)+48);
        randomArr2.push(Math.floor(Math.random()*8)+28);
        randomArr3.push(Math.floor(Math.random()*9)+15);
      }
      this.option6.series[0].data=randomArr1; this.option6.series[1].data=randomArr2; this.option6.series[2].data=randomArr3
      mychart1.setOption(this.option6)
      //chart2
      mychart2.clear()
      mychart2.setOption(this.option5)
      //chart3
      mychart3.clear()
      let hotelArr1=[]; let hotelArr2=[]; this.option9.series[0].data[0]=[]; this.option9.series[1].data[0]=[]
      for(let i=0;i<10;i++) {
        hotelArr1.push(Math.floor(Math.random()*4)+2);
        hotelArr2.push(Math.floor(Math.random()*5)+3);
      }
      this.option9.series[0].data[0]=hotelArr1; this.option9.series[1].data[0]=hotelArr2
      mychart3.setOption(this.option9)
      mychart4.clear()
      let temDataArr1=[]; let rainDataArr2=[]; let humDataArr3=[];
      for(let i=0;i<12;i++) {
        temDataArr1.push(Math.floor(Math.random()*4)+24);
        rainDataArr2.push(Math.floor(Math.random()*5+1));
        humDataArr3.push(Math.floor(Math.random()*29)+35);
      }
      this.option8.series[0].data=temDataArr1; this.option8.series[1].data=rainDataArr2; this.option8.series[2].data=humDataArr3
      mychart4.setOption(this.option8)

    },

    //级联选择器
    handleChange(value) {

    },


  }
};
</script>

<style lang="scss">
.customerDialogClass {
  width: 900px;
  height: 800px;
}
::v-deep .el-dialog {
  background-color: rgba(34, 52, 87, 0.8);
}
</style>


